import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: generatorSqlmapCustom
 * @description: 模板代码生成工具类
 * @author: 顾小皓
 * @create: 2020-07-19 16:18
 **/
public class TemplatesUtils {
    public static void execute(Map root, String templateUrl, String filePath) throws Exception {
        // 创建Freemarker配置实例
        Configuration cfg = new Configuration();
        cfg.setDefaultEncoding("UTF-8");
        cfg.setDirectoryForTemplateLoading(new File("resources/templates/"));
        // 创建数据模型设置参数(树模型，只有一个根元素)
//        installPublicParams(root, enumsModel);
        // 加载模板文件
        Template tl = cfg.getTemplate(templateUrl);
        //添加公共参数
        TemplatesEmus.addPubMsg(root);

        /*Writer out = new StringWriter(1024);
        tl.process(root, out);
        out.flush();
        out.close();
        System.out.println(out.toString());*/

        Writer out = new OutputStreamWriter(new FileOutputStream(filePath));
        tl.process(root, out);
        out.flush();
        out.close();
    }

    /**
     * 生成dao接口整合
     *
     * @throws Exception
     */
    public static void getAutoMapper() throws Exception {
        List<String> tableName = PaginationPlugin.tableName;
        Map<String, String> tableRemarks = PaginationPlugin.tableRemarks;
        HashMap autoMapper = new HashMap();
        HashMap<Object, Object> model = new HashMap<>();
        model.put("keyValues", tableName);
        autoMapper.put("model", model);
        autoMapper.put("packageEnums", TemplatesEmus.AutoMapper);
        autoMapper.put("tableRemarksMap", tableRemarks);

        //文件路径
        String path = TemplatesEmus.AutoMapperPath;
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            System.out.println("目录不存在");
            System.out.println(file.mkdirs());
        } else {
            System.out.println("目录存在");
        }
        execute(autoMapper, TemplatesEmus.AutoDaoTemplateUrl, path+"AutoDao.java");
    }

    /**
     * 生成Service接口整合
     *
     * @throws Exception
     */
    public static void getCustomizeServiceImpl() throws Exception {
        List<String> tableName = PaginationPlugin.tableName;
        Map<String, String> tableRemarks = PaginationPlugin.tableRemarks;
        HashMap autoMapper = new HashMap();
        HashMap<Object, Object> model = new HashMap<>();
        model.put("keyValues", tableName);
        autoMapper.put("model", model);
        autoMapper.put("packageEnums", TemplatesEmus.AutoService);
        autoMapper.put("tableRemarksMap", tableRemarks);

        String path = TemplatesEmus.AutoServicePath;
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            System.out.println("目录不存在");
            System.out.println(file.mkdirs());
        } else {
            System.out.println("目录存在");
        }
        execute(autoMapper, TemplatesEmus.AutoServiceTemplateUrl, path+"AutoService.java");
    }

    /**
     * 生成IService接口列表
     *
     * @throws Exception
     */
    public static void getIService() throws Exception {
        List<String> tableNames = PaginationPlugin.tableName;
        Map<String, String> tableRemarks = PaginationPlugin.tableRemarks;
        for (String tableName : tableNames) {
            HashMap autoMapper = new HashMap();
            HashMap<Object, Object> model = new HashMap<>();
            //添加更多update方法
            Map<String, List<TableFieldsModel>> tablesFields = PaginationPlugin.tablesFields;
            model.put("tablesFields",tablesFields.get(tableName));
            //添加需要import的包
            Map<String,List<String>> mapFullyQualifiedNames = PaginationPlugin.mapFullyQualifiedNames;
            model.put("listFullyQualifiedName",mapFullyQualifiedNames.get(tableName));
            model.put("pojo", tableName);
            //添加公共数据
            autoMapper.put("model", model);
            autoMapper.put("packageEnums", TemplatesEmus.IService);
            autoMapper.put("className", "I" + tableName + "Service");
            autoMapper.put("tableRemarks",tableRemarks.get(tableName));

            String path = TemplatesEmus.IServicePath;
            File file = new File(path);
            if (!file.exists() && !file.isDirectory()) {
                System.out.println("目录不存在");
                System.out.println(file.mkdirs());
            } else {
                System.out.println("目录存在");
            }
            execute(autoMapper, TemplatesEmus.IAutoServiceTemplateUrl, path+"I" + tableName + "Service.java");
        }
    }

    /**
     * 生成Service实现列表
     *
     * @throws Exception
     */
    public static void getServiceImpl() throws Exception {
        List<String> tableNames = PaginationPlugin.tableName;
        Map<String, String> tableRemarks = PaginationPlugin.tableRemarks;
        for (String tableName : tableNames) {
            HashMap autoMapper = new HashMap();
            HashMap<Object, Object> model = new HashMap<>();
            //添加更多update方法
            Map<String, List<TableFieldsModel>> tablesFields = PaginationPlugin.tablesFields;
            model.put("tablesFields",tablesFields.get(tableName));
            //添加需要import的包
            Map<String,List<String>> mapFullyQualifiedNames = PaginationPlugin.mapFullyQualifiedNames;
            model.put("listFullyQualifiedName",mapFullyQualifiedNames.get(tableName));
            //添加公共数据
            model.put("pojo", tableName);
            autoMapper.put("model", model);
            autoMapper.put("packageEnums", TemplatesEmus.ServiceImpl);
            autoMapper.put("className", tableName + "ServiceImpl");
            autoMapper.put("tableRemarks", tableRemarks.get(tableName));

            String path = TemplatesEmus.ServiceImplPath;
            File file = new File(path);
            if (!file.exists() && !file.isDirectory()) {
                System.out.println("目录不存在");
                System.out.println(file.mkdirs());
            } else {
                System.out.println("目录存在");
            }
            execute(autoMapper, TemplatesEmus.AutoServiceImplTemplateUrl, path +tableName+"ServiceImpl.java");
        }
    }
    /**
     * 生成Dao层通用方法
     *
     * @throws Exception
     */
    public static void getBaseMapper() throws Exception {
        HashMap autoMapper = new HashMap();
        autoMapper.put("packageEnums", TemplatesEmus.BaseMapper);

        //文件路径
        String path = TemplatesEmus.BaseMapperPath;
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            System.out.println("目录不存在");
            System.out.println(file.mkdirs());
        } else {
            System.out.println("目录存在");
        }
        execute(autoMapper, TemplatesEmus.BaseMapperTemplateUrl, path+"BaseMapper.java");
    }
}
